#include "dspSimulator/instruction/MV.h"

namespace dspsimulator {

    std::shared_ptr<DspInstruction>
    MV::decode_standard_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<MV> mv;
        std::bitset<32> &data = dsp_instruction_info->m_standard_bits;
        if (common::check_bits<5>(data, 0b00000, 13) &&
            common::check_bits<10>(data, 0b0001101000, 2) &&
            exist_standard_condition(common::extract_bits<4>(data, 28))) {
            mv = std::make_shared<MV>(dsp_instruction_info->address(), 4);
            mv->m_creg_z = common::extract_bits<4>(data, 28);
            u32 dst = common::extract_bits<5>(data, 23).to_ulong();
            u32 src2 = common::extract_bits<5>(data, 18).to_ulong();
            bool x = data[12];
            bool s = data[1];
            bool p = data[0];
            mv->m_func_unit = s ? FuncUnit::S1 : FuncUnit::S2;
            mv->m_op1 = std::make_shared<SingleReg>(get_src2_reg32_kind(src2, s, x));
            mv->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
            mv->m_p = p;
        } else if (exist_standard_condition(common::extract_bits<4>(data, 28)) &&
                   common::check_bits<16>(data, 0b0000001001010000, 2)) {
            mv = std::make_shared<MV>(dsp_instruction_info->address(), 4);
            mv->m_creg_z = common::extract_bits<4>(data, 28);
            u32 dst = common::extract_bits<5>(data, 23).to_ulong();
            u32 src2 = common::extract_bits<5>(data, 18).to_ulong();
            bool s = data[1];
            bool p = data[0];
            mv->m_func_unit = s ? FuncUnit::D1 : FuncUnit::D2;
            mv->m_op1 = std::make_shared<SingleReg>(get_src2_reg32_kind(src2, s));
            mv->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
            mv->m_p = p;
        }
        return mv;
    }

    std::shared_ptr<DspInstruction>
    MV::decode_compact_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<MV> mv;
        std::bitset<16> &data = dsp_instruction_info->m_compact_bits;
        if (common::check_bits<2>(data, 0b11, 1) && common::check_bits<2>(data, 0b10, 5)) {
            // G-2
            std::bitset<2> unit = common::extract_bits<2>(data, 3);
            if (unit == 0b11) {
                return nullptr;
            }
            mv = std::make_shared<MV>(dsp_instruction_info->address(), 2);
            bool s = data[0];
            if (unit == 0b00) {
                mv->m_func_unit = s ? FuncUnit::L1 : FuncUnit::L2;
            } else if (unit == 0b01) {
                mv->m_func_unit = s ? FuncUnit::S1 : FuncUnit::S2;
            } else {
                mv->m_func_unit = s ? FuncUnit::D1 : FuncUnit::D2;
            }
            bool x = data[12];
            u32 src2 = common::extract_bits<3>(data, 7).to_ulong();
            u32 dst = common::extract_bits<3>(data, 13).to_ulong();
            dst += common::extract_bits<2>(data, 10).to_ulong() << 3;
            mv->m_op1 = std::make_shared<SingleReg>(get_src2_reg32_kind(src2, s, x));
            mv->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
            mv->m_p = dsp_instruction_info->p();
        }
        return mv;
    }

    std::string MV::to_string() const {
        return get_standard_condition(m_creg_z) + m_name + " " + function_unit_names[static_cast<u32>(m_func_unit)] +
               " " + m_op1->to_string() + "," + m_op2->to_string();
    }

    void MV::generate_ir(const std::shared_ptr<simulator::IRGenElement> &ir_gen_element) {
        std::shared_ptr<simulator::LLVMJit> llvm_jit = ir_gen_element->m_llvm_jit;
        RegIRCopy &reg_ir_copy = ir_gen_element->m_reg_ir_copy;
        std::shared_ptr<llvm::IRBuilder<>> ir_builder = llvm_jit->ir_builder();

        // mv .func src,dst
        auto op1 = std::dynamic_pointer_cast<SingleReg>(m_op1);
        auto op2 = std::dynamic_pointer_cast<SingleReg>(m_op2);
        if (op1 != nullptr && op2 != nullptr) {
            std::string src_reg_name = get_reg_name(op1->reg());
            llvm::Value *src = reg_ir_copy[src_reg_name];
            std::string dst_reg_name = get_reg_name(op2->reg());
            llvm::Value *dst_ptr = IRGenerator::get_global_value_ptr(llvm_jit, dst_reg_name);
            ir_builder->CreateStore(src, dst_ptr);
        }
    }
}